﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace TD.TheGame
{
    public class Archer : Sprite
    {
        protected Rectangle[] walk;
        protected Rectangle[] shoot;

        public Archer(Game game, Vector2 position, bool active, int maxLoopTime, SpriteEnumState state)
            : base(game, position, active, maxLoopTime, state)
        {
            this.maxLoopTime = maxLoopTime;
            walk = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "Archer\\" + "archerNormal.xml");
            shoot = animationHandler.animationRec(Environment.CurrentDirectory + "\\XML\\" + "Archer\\" + "archerShoot.xml");
            current = walk;
        }
        
        public override void Update(GameTime gameTime)
        {
            //Check if State is changed, if changed: begin with the first sprite of the new array
            if (oldState != State)
            {
                if (State == SpriteEnumState.Normal)
                {
                    current = walk;
                }
                if (State == SpriteEnumState.Shoot)
                {
                    current = shoot;
                }
                //set the currentsprite different
                currentRect = 0;
            }
            oldState = State;
            Shoot();
            Move();
            Collison();
            base.Update(gameTime);
        }

        public void Shoot()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                State = SpriteEnumState.Shoot;
            }
            if (State == SpriteEnumState.Shoot && endSprite)
            {
                State = SpriteEnumState.Normal;
            }
        }

        public void Collison()
        {
            if (position.X > 100)
            {
                position.X = 100;
            }
            if (position.X < 0)
            {
                position.X = 0;
            }
            if (position.Y > Game.Window.ClientBounds.Height - BoundingRectangle.Height)
            {
                position.Y = Game.Window.ClientBounds.Height - BoundingRectangle.Height;
            }
            if (position.Y < Game.Window.ClientBounds.Height - BoundingRectangle.Height - 175)
            {
                position.Y = Game.Window.ClientBounds.Height - BoundingRectangle.Height - 175;
            }
        }

        private void Move()
        {
            //let player walk
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
            {
                position.Y += 2;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
            {
                position.Y -= 2;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                position.X -= 2;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
            {
                position.X += 2;
            }
        }

        protected override Texture2D getTexture()
        {
            //Init texture
            manager.LoadAsset("level\\Archer");
            texture = manager.UseAsset("level\\Archer");
            return texture;
        }

        public override void Reset()
        {
            maxLoopTime = 500;
        }
    }
}
